<?php
/*
 * 图片常用类
 * @package    system
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 2eed325a4fa693412ca434599495932b40ef758c $
 */

/**
 * 静态类函数, 便于组织
 * Class Picture
 */
class Picture {

	private static $_domain = U::DOMAIN_IMG1;

	/**
	 * 根据给定的URL, 在指定位置生成指定大小的缩略图, 然后返回地址
	 * 生成缩略图函数
	 * @param        $imgurl 图片路径
	 * @param int    $width  缩略图宽度
	 * @param int    $height 缩略图高度
	 * @param int    $resize 1, 比例, 0 切割
	 * @param string $nopic  无图片是默认图片路径
	 * @return string
	 */
	public static function thumb($imgurl, $width, $height, $resize = 1, $nopic = 'nopic.gif') {
		if (empty($imgurl)) {
			$path  = IMG_PATH . '/' . $nopic;
			$tPath = self::thumbName(IMG_PATH . '/' . $nopic, $width, $height);
			if (!file_exists($tPath)) {
				$image = new image($path);
				$image->thumb($width, $height, $resize, $tPath);
			}
			$tname = str_replace(IMG_PATH . '/', '', $tPath);
			return IMG_URL . $tname;
		}
		$imgurl = self::basename($imgurl);
		$aimUrl = self::thumbName($imgurl, $width, $height);
		// 检测是否存在 缩略图
		if (!self::_isExists($aimUrl)) {
			// 检测是否存在 源文件
			if (!self::_isExists($imgurl)) { // 不存在源文件 handle nopic
				$path  = IMG_PATH . '/' . $nopic;
				$tPath = self::thumbName($path, $width, $height);
				if (!file_exists($tPath)) {
					$image = new image($path);
					$image->thumb($width, $height, $resize, $tPath);
				}
				$tname = str_replace(IMG_PATH . '/', '', $tPath);
				return IMG_URL . $tname;
			} else { //
				self::resize($imgurl, $width, $height, $resize);
			}
		}
		return $aimUrl;
	}


	public static function resize($imgUrl, $width, $height, $resize) {
		$source = self::realpath($imgUrl);
		$aim    = self::thumbName($source, $width, $height);
		$image  = new image($source);
		$image->thumb($width, $height, $resize, $aim);
	}

	private static function _isExists($url) {
		return file_exists(self::realpath($url));

	}

	/**
	 * 返回完整的URL, 目前支持一个域名
	 * @param $realpath
	 * @return string
	 */
	public static function url($realpath) {
		return U::domain(U::DOMAIN_IMG1) . self::relative($realpath);
	}

	/**
	 * 生成图片相对名称和位置
	 */
	public static function name($moduleid, $userid, $ext, $time = 0) {
		global $MODULE, $DT_TIME;
		$time    = $time ? $time : $DT_TIME;
		$modname = $MODULE[$moduleid]['module'];
		return
			substr($modname, 0, 2) . date('/Ym/d/', $time) .
			base_convert($userid, 10, 36) . '_' . base_convert($DT_TIME, 10, 36) . '_' . Str::random(3) . '.' .
			$ext;
	}

	/**
	 * 删除图片和关联的缩略图, 如果给定的是缩略图, 则忽略大图, 给定大图, 删除所有小图
	 * @param $url
	 * @return bool
	 */
	public static function delete($url) {
		$basename = basename($url);

		if (self::isThumb($basename)) {
			$path = self::realpath($url);
			File::del($path);
		} else {
			// 查找相关联的文件
			$realpath = self::realpath($url);
			$back     = '.' . File::ext($realpath);
			$realpath = substr($realpath, 0, strrpos($realpath, $back));
			if ($realpath == '') {
				return false;
			}
			$files = glob($realpath . '*');
			if ($files) {
				foreach ($files as $file) {
					File::del($file);
				}
			}
		}
		return true;
	}

	public static function deleteFtp($file) {
		global $CFG;
		$path = self::relative($file);
		$ftp  = new Ftp($CFG['ftpImg']);
		try {
			$ftp->delete($path);
		} catch (FtpException $e) {

		}
	}

	/**
	 * 删除所有图片, 包含缩略图和原图
	 * @param $url
	 */
	public static function deleteAll($url) {
		if ($url) {
			$url = self::basename($url);
			return self::delete($url);
		}
	}

	/**
	 * 返回相对路径
	 * @param $url
	 * @return mixed
	 */
	public static function relative($url) {
		$path = str_replace(U::realpath(U::DOMAIN_IMG1) . '/', '', $url);
		return str_replace(U::domain(U::DOMAIN_IMG1), '', $path);
	}

	/**
	 * 返回缩略图名称
	 * @param $basename
	 * @param $width
	 * @param $height
	 * @return string
	 */
	public static function thumbName($basename, $width, $height) {
		$back = '.' . File::ext($basename);
		$base = substr($basename, 0, strrpos($basename, $back));
		return $base . '_t_' . $width . '_' . $height . $back;
	}

	/**
	 * 检测图片是否是缩略图
	 * @param $basename
	 * @return bool
	 */
	public static function isThumb($basename) {
		return strpos($basename, '_t_') !== false;
	}

	/**
	 * 根据传递的名称返回基础名称
	 * @param $anyname
	 * @return mixed
	 */
	public static function basename($anyname) {
		if (strpos($anyname, '_t_') !== false) {
			return preg_replace("/(.*?)_t_\d+_\d+\.(.{2,6})/", "$1.$2", $anyname);
		} else {
			return $anyname;
		}
	}

	/**
	 * 返回任何路径的真实地址
	 * @param $path
	 * @return mixed
	 */
	public static function realpath($path) {
		if (strpos($path, U::domain(self::$_domain)) !== false) { // url
			$realpath = str_replace(U::domain(self::$_domain), U::realpath(self::$_domain) . '/', $path);
		} else if (strpos($path, U::realpath(self::$_domain)) !== false) { // realpath
			$realpath = $path;
		} else {
			$realpath = U::realpath(self::$_domain) . '/' . $path;
		}
		return str_replace('//', '/', $realpath);
	}

	/**
	 * 保存文件.
	 * @param $tmpname
	 * @param $localaim
	 * @return bool
	 */
	public static function saveFile($tmpname, $localaim) {
		return File::copyto($tmpname, $localaim);
	}

	/**
	 * 检测是否是用户名
	 * @param $file
	 * @return int
	 */
	public static function userid($file) {
		$file = basename($file);
		if (preg_match("/^([0-9a-zA-Z]+)_(.+)\..{2,4}/", $file, $m)) {
			return base_convert($m[1], 36, 10);
		} else {
			return 0;
		}
	}

	/**
	 * 保存URL地址到指定目录
	 * @param $url
	 * @param $aim
	 * @param $ref
	 */
	public static function saveRemote($url, $aim, $ref = '') {
		$localTmp = DT_CACHE . '/ftp/';
		$fileTmp  = $localTmp . basename($aim);
		if (File::curlGet($url, $fileTmp, $ref)) {
			self::saveFile($fileTmp, $aim);
		}
		File::del($fileTmp);
	}

}